package com.iyao_app.mvp.preview;

import android.graphics.drawable.Animatable;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.backends.pipeline.PipelineDraweeController;
import com.facebook.drawee.controller.BaseControllerListener;
import com.facebook.drawee.controller.ControllerListener;
import com.facebook.imagepipeline.image.ImageInfo;
import com.iyao_app.R;
import com.iyao_app.preview.OnPhotoTapListener;
import com.iyao_app.preview.PhotoDraweeView;
import com.iyao_app.util.Contants;

import butterknife.OnClick;

/**
 * 作者 :  胡家兵
 * 时间 :  2017/11/27.
 */

public class ImageViewActivity extends AppCompatActivity {

    PhotoDraweeView mPhotoDraweeView;
    TextView mTvSize;
    LinearLayout mLinDown;

    private String imageUrl;
//    private int mWidth = 0;
//    private int mHeight = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
        setContentView(R.layout.activity_image_view);
        mPhotoDraweeView= findViewById(R.id.photoView);
        mTvSize= findViewById(R.id.tv_size);
        mLinDown= findViewById(R.id.lin_down);
        imageUrl = getIntent().getStringExtra(Contants.TYPE);
        initData();
    }

    private void initData() {
        if (!TextUtils.isEmpty(imageUrl)) {
//            mWidth = ScreenUtils.getScreenWidth();

            //添加点击事件
            mPhotoDraweeView.setOnPhotoTapListener(new OnPhotoTapListener() {
                @Override
                public void onPhotoTap(View view, float x, float y) {
                    finish();
                }
            });
            if (!TextUtils.isEmpty(imageUrl)) {


                ControllerListener controllerListener = new BaseControllerListener<ImageInfo>() {
                    @Override
                    public void onFinalImageSet(String id, @Nullable ImageInfo imageInfo, @Nullable Animatable anim) {
                        if (imageInfo == null || mPhotoDraweeView == null) {
                            return;
                        }
//                        QualityInfo qualityInfo = imageInfo.getQualityInfo();
//                        LogUtil.e("==qualityInfo==" + qualityInfo.getQuality());
//                        mHeight = (int) ((float) (mWidth * imageInfo.getHeight()) / (float) imageInfo.getWidth());
                        mPhotoDraweeView.update(imageInfo.getWidth(), imageInfo.getHeight());
                    }

                    @Override
                    public void onIntermediateImageSet(String id, @Nullable ImageInfo imageInfo) {

                    }

                    @Override
                    public void onFailure(String id, Throwable throwable) {
                        throwable.printStackTrace();
                    }
                };

                try {

                    PipelineDraweeController controller = (PipelineDraweeController) Fresco.newDraweeControllerBuilder()
                            .setOldController(mPhotoDraweeView.getController())
                            .setUri(Uri.parse(imageUrl))
                            .setControllerListener(controllerListener)
                            .build();
                    mPhotoDraweeView.setController(controller);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }
    }

    @OnClick(R.id.lin_down)
    public void onViewClicked() {

    }
}
